package com.oocl.tm.ui;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.oocl.tm.protocol.MsgPackage;
import com.oocl.tm.protocol.Operation;

public class Login extends JFrame implements ActionListener {
	private JTextField nameTf;
	private JPasswordField  psdPf;
	private JButton loginBtn;
	private JButton cancelBtn;
	private JLabel nameLb;
	private JLabel psdLb;
	private JLabel title;
	
	public Login() {
		this.setSize(380, 300);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		
		init();
		addEvent();
		this.setVisible(true);
	}
	
	private void init(){
		this.setLayout(null);
		nameTf=new JTextField();
		psdPf=new JPasswordField();
		loginBtn=new JButton("login");
		cancelBtn=new JButton("register");
		nameLb=new JLabel("Acount:");
		psdLb=new JLabel("Password:");
		title=new JLabel("TM Chat");
		title.setFont(new Font("Dialog", 1, 16));
		title.setBounds(160, 0, 70, 100);
		title.setForeground(Color.red);
		
		nameLb.setBounds(50, 90, 50, 25);
		psdLb.setBounds(30, 150, 100, 25);
		nameTf.setBounds(100, 80, 200, 40);
		psdPf.setBounds(100, 140, 200, 40);
		
		loginBtn.setBounds(70, 200, 90, 40);
		cancelBtn.setBounds(220, 200, 90, 40);
		this.add(nameTf);
		this.add(psdPf);
		this.add(loginBtn);
		this.add(cancelBtn);
		this.add(nameLb);
		this.add(psdLb);
		this.add(title);
	}
	
	private void addEvent(){
		loginBtn.addActionListener(this);
		cancelBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				Register.getRegister();
				
			}
		});
	}
	
	public static void main(String[] args) {
		new Login();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		try {
			if(!nameTf.getText().equals("")){
				System.out.println("login......");
				
				Socket socket = new Socket("10.222.29.165", 8888);
				ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
				ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
				String name = nameTf.getText();
				MsgPackage msgPackage = new MsgPackage(Operation.LOGIN, name, null, null, new Date(), null);
				out.writeObject(msgPackage);
				System.out.println(name + " is login sucess!");
				
				new ChatFrame(socket, name, out, in, this);
			}else{
				new LoginFail();
			}
		} catch (IOException e1) {
			new LoginFail();
			System.out.println("Connection refused: connect");
		}
		
		
	}
}